package com.qf.mybatis01.utils;

import com.qf.mybatis01.entity.Book;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class QfFileAppender {
    private String filepath;
    private BufferedWriter bufferedWriter;

    public QfFileAppender(String filepath) throws IOException {
        this.filepath = filepath;
        this.bufferedWriter = new BufferedWriter(new FileWriter(filepath, true));

    }

    public Integer appendListToFile(List<String> data) throws IOException {
        int count=0;
        for (String line : data) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
            count++;
        }
        return count;
    }
    public void close() throws IOException {
        bufferedWriter.close();
    }

    public static void main(String[] args) throws IOException {
        QfFileAppender qfFileAppender = new QfFileAppender("C:\\test\\books.txt");
        //List<String> list = Arrays.asList("asd", "asasa", "qwqwqw");
        List<String> list = Book.randomBookCSVList(10);
        Integer result = qfFileAppender.appendListToFile(list);
        System.out.println(result);
        qfFileAppender.close();
    }
}
